package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.Product;
import com.blb.entity.Sort;
import com.blb.entity.User;
import com.blb.service.IProductService;
import com.blb.service.ISortService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *商品管理的分类管理
 * @author wss
 * @since 2020-05-21
 */
@Controller
@RequestMapping("/sort")
public class SortController {

    @Autowired
    private ISortService iSortService;

    @Autowired
    private IProductService iProductService;

    @RequiresPermissions("classfyied")
    @GetMapping("sortment")
    public String sortment() {
        return "product/sortment";
    }
    @RequiresPermissions("classfyied")
    @GetMapping("addtypes")
    public String addtypes() {
        return "product/addtypes";
    }
    @RequiresPermissions("classfyied")
    @GetMapping("addtypes2")
    public String addtypes2() {
        return "addtypes1";
    }
    @RequiresPermissions("classfyied")
    @GetMapping("addtypes3")
    public String addtypes3() {
        return "product/addtypes3";
    }

    //添加一级分类
    @RequiresPermissions("classfyied")
    @ResponseBody
    @RequestMapping("/addsort")
    public String addSort(Sort sort) {
        sort.setPid("0");
        sort.setSortType("1");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        sort.setCompanyId(user.getCompany().getId());
        int a = iSortService.addSort(sort);
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }

    //添加二级分类
    @RequiresPermissions("role")
    @ResponseBody
    @RequestMapping("/addsorts")
    public String addSorts(Sort sort) {
        sort.setSortType("2");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        sort.setCompanyId(user.getCompany().getId());
        int a = iSortService.addSorts(sort);
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }
    @RequiresPermissions("classfyied")
    @ResponseBody
    @RequestMapping("/sortById")
    public String sortById(String id) {
        int b = iSortService.sortByIds(id);
        if (b > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }
    @RequiresPermissions("classfyied")
    @ResponseBody
    @RequestMapping("/sortByName")
    public String sortByName(String sortName) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int b = iSortService.sortByName(sortName, user.getCompany().getId());
        if (b > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }

    //添加三级分类
    @RequiresPermissions("classfyied")
    @ResponseBody
    @RequestMapping("/addsortss")
    public String addSortsss(Sort sort) {
        sort.setSortType("3");
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        sort.setCompanyId(user.getCompany().getId());
        int a = iSortService.addSortss(sort);
        if (a > 0) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }


    //分类查询
    @RequiresPermissions("classfyied")
    @ResponseBody
    @RequestMapping("/treeSort")
    public List<Sort> sortAll() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<Sort> sortList = iSortService.sortAll(user.getCompany().getId());
        return sortList;
    }

    //修改传参
    @RequiresPermissions("classfyied")
    @RequestMapping("/updTree")
    public String upd(WebRequest req, Model model) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String id = req.getParameter("sortId").trim();
        Sort sort = iSortService.sortById(id, user.getCompany().getId());
        model.addAttribute("sorts", sort);
        String type = sort.getSortType();
        if (type.equals("1")) {
            return "product/addtypes1";
        } else if (type.equals("2")) {
            return "product/addtypes2";
        } else {
            return "product/addtypes3";
        }
    }
    @RequiresPermissions("classfyied")
    @ResponseBody
    @RequestMapping("/sortUpdate")
    public String sortUpdate(Sort sort) {
        boolean result = iSortService.updateById(sort);
        if (result == false) {
            return "{\"result\":false}";
        } else {
            return "{\"result\":true}";
        }
    }
    @RequiresPermissions("classfyied:deleted")

    @ResponseBody
    @GetMapping("/sortDelete")
    public String del(String id) {
        List<Product> pro = iProductService.list(new QueryWrapper<Product>().eq("sortss_id", id));
        if (pro.size() > 0) {
            return "{\"result\":\"no\"}";
        }
        if (iSortService.removeById(id)) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }

    @ResponseBody
    @GetMapping("/sortsDelete")
    public String dels(String id) {
        List<Sort> list = iSortService.list(new QueryWrapper<Sort>().eq("pid", id));
        if (list.size() > 0){
            return "{\"result\":\"no\"}";
        }
        if (iSortService.removeById(id)) {
            return "{\"result\":true}";
        } else {
            return "{\"result\":false}";
        }
    }


}

